package com.dk.logc.business;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import com.dk.logc.config.CountFactory;
import com.dk.logc.domain.CountRole;
import com.dk.logc.domain.CountRoleSum;
import com.dk.logc.domain.FlogLogin;
import com.dk.logc.domain.GameRole;
import com.dk.logc.kafka.base.BaseRoleFlog;
import com.dk.logc.service.CountRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @Author 林道敬
 * @Date 2024-01-11
 * @Description
 **/
@Component
@Slf4j
public class FlogLoginBusiness implements SuperBusiness{
    @PostConstruct
    public void init(){
        CountFactory.register("login",this);
    }

    @Autowired
    private GameRoleBusiness gameRoleBusiness;
    @Autowired
    private CountRoleService countRoleService;


    @Override
    @Transactional
    public void progress(BaseRoleFlog baseRoleFlog, CountRole countRole, CountRoleSum countRoleSum){
        try{
            FlogLogin flogLogin = (FlogLogin) baseRoleFlog;
            GameRole gameRole = gameRoleBusiness.getGameRole(flogLogin,"search GameRole");
            if(gameRole == null){
                return ;
            }
            Date createRoleDate = gameRole.getCreateTime();
            Date lastLoginTime  = gameRole.getLastLoginTime();
            //是否允许统计(一天只统计一次)
            if (lastLoginTime== null || !DateUtil.isSameDay(new Date(),lastLoginTime)){
                //相差天数
                long day = DateUtil.betweenDay(createRoleDate, new Date(), true);
                // 老玩家 = day >0
                if(day > 0){
                    countRole.setOldLoginNum(countRole.getOldLoginNum()+1);
                }else {
                    countRole.setNewLoginNum(countRole.getNewLoginNum()+1);
                }
                countRoleService.saveOrUpdate(countRole);
                if (day == 1 || day == 3 || day == 5 || day == 7 || day == 15 || day == 30 || day == 60 || day == 90 || day == 180) {
                    String dateStr = "login_day" + day;
                    SqlRunner.db()
                            .update("update count_role set "+dateStr+"=("+dateStr +"+1) WHERE id="+countRole.getId());
                }
                gameRole.setLastLoginTime(new Date());
                gameRoleBusiness.saveOrUpdate(gameRole);
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }

    public boolean isSameDate(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("ymd",new Date());
        long count = countRoleService.list(queryWrapper).stream().count();
        if(count >= 1){
            return true;
        }
        return false;
    }
}
